<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>修改密码</title>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">修改密码</div>
                        <div class="layui-card-body">
                            <form class="layui-form" action="">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">用户名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="username" lay-verify="username" autocomplete="off" placeholder="请输入用户名" class="layui-input" value="ceshi-000" readonly>
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">不可修改</div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">当前密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入当前密码" class="layui-input" value="">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">新密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="new_password" lay-verify="new_password" autocomplete="off" placeholder="请输入新密码" class="layui-input" value="" id="new_password">
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">确认新密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="new_password2" lay-verify="new_password2" autocomplete="off" placeholder="请再次输入新密码" class="layui-input" value="">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <a class="layui-btn" lay-submit lay-filter="formSubmit">确认修改</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form'], function(){
            var $ = layui.$
                ,form = layui.form;

            form.verify({
                password: function (value) {
                    if (value.length < 1) {
                        return '请输入当前密码';
                    }
                }
                , new_password: function (value) {
                    if (value.length < 1) {
                        return '请输入新密码';
                    } else if (value.length < 6 || value.length > 16) {
                        return '请输入6到16个字符';
                    }
                }
                , new_password2: function (value) {
                    if (value.length < 1) {
                        return '请再次输入新密码';
                    }
                    var new_password = $('#new_password').val();
                    if (value !== new_password) {
                        return '两次输入的新密码不一致';
                    }
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function(data){
                layer.msg('提交中', {
                    shadeClose: false
                    ,icon: 16
                    ,shade: 0.01
                });
                $.ajax({
                    type: "POST",
                    url: "/account/savePassword",
                    data: data.field,
                    success:function(response){
                        var dataObj=$.parseJSON(response);
                        if (dataObj.code===0) {
                            layer.msg('数据提交成功', {icon: 6});
                            setTimeout(function(){
                                location.href = '/account/changePassword';
                            }, 1000);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {
                        layer.msg('哦噢，网络开小差了', {icon: 5});
                    }
                });
                return false;
            });
        });
    </script>
</div>
</body>
</html>
